import { createMD5 } from "hash-wasm";
import { IHasher } from "hash-wasm/dist/lib/WASMInterface";

let md5: IHasher | null = null;
self.onmessage = async (e) => {
  if (!md5) {
    md5 = await createMD5();
    md5.init();
  }

  if (e.data.done) {
    const hash = md5.digest();
    self.postMessage({ done: true, hash });
    self.close();
  } else {
    md5.update(new Uint8Array(await e.data.blob.arrayBuffer()));
    self.postMessage({ done: false, progress: e.data.name });
  }
};
